#ifndef GRPCEVENTSERVERSERVICE_H
#define GRPCEVENTSERVERSERVICE_H

#include <algorithm>
#include <chrono>
#include <cmath>
#include <iostream>
#include <memory>
#include <string>
#include <map>

#include <grpc/grpc.h>
#include <grpcpp/security/server_credentials.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/server_context.h>

#include "grpcevent.grpc.pb.h"

using grpc::CallbackServerContext;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::Status;
using Evol::Common::Grpc::Server::GrpcEvent;
using Evol::Common::Grpc::Server::GrpcEventMessage;

/***************************************************************
*  @Copyright:  Copyright (c) 2024 LK. All rights reserved.
*  @FileName:   grpceventserverservice.h
*  @Brief:      grpc 事件服务(特定的功能)
*  @Author:     HXJ
*  @Date:       2024-08-17
****************************************************************/

class GrpcClientProxyInService;
class GrpcEventServerService final : public GrpcEvent::CallbackService {
public:
    GrpcEventServerService();

    //发送事件
    void SendEvent(const std::string& client, const std::string& jsonText);

    //流 结束，客户端断开清理
    void ClientEndFlow(std::string name);

    //分发事件
    grpc::ServerUnaryReactor* Dispatch(CallbackServerContext* context,
                                         const GrpcEventMessage* request,
                                       GrpcEventMessage* response) override;


    grpc::ServerWriteReactor<GrpcEventMessage>* SubscribeDispatch(
        CallbackServerContext* context,
        const GrpcEventMessage* request) override;


private:
    std::mutex m_mutex;
    std::map<std::string, GrpcClientProxyInService*> m_clients; //客户端代理
};

extern std::shared_ptr<GrpcEventServerService> g_pEventService;

#endif // GRPCEVENTSERVERSERVICE_H
